package com.idea.rdftree;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.swing.border.BevelBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import com.hp.hpl.jena.mem.ModelMem;
import com.hp.hpl.jena.rdf.model.Model;
import com.idea.common.swing.treetable.JTreeTable;
//import com.idea.graphic.diagram.model.Diagram;

/**
 * @author danny
 *
 * created : 05-Nov-2002
 *
 * (c) D.Ayers 2002
 */
public class RdfTreePanel extends JPanel implements TreeSelectionListener {

	private JTree tree;

	private RdfTreeModel treeModel;

	//private JScrollPane treeScroll;
	private String selectedUrl;

	protected Action action;
	protected JPopupMenu popupMenu;
	protected TreePath clickedPath;
	//	private String initFilename=null;

	private Model topModel;
	private String filename;
	private boolean withTable;
	private EditMenu editMenu;

	public RdfTreePanel(String filename) {
		this();
		//	initFilename = filename;
		this.filename = filename;
	}

	public RdfTreePanel(boolean withTable) {
		super(new BorderLayout());
		//super(new GridLayout(1, 1));
		//super();
		this.withTable = withTable;
	}

	public RdfTreePanel() {
		this(false);
		//	this.organizer = organizer;
		//	manager = getOrganizer().getProjectManager();
		//	init();
	}

	JTreeTable treeTable;

	public void init() {
		clear();
		//	initTreeModel();
		createTreeModel(getTopModel(), getTopModelFilename());

		//     treeTable = new JTreeTable(treeModel);
		//		TreeTableModel  model = createModel(path);

		if (withTable) {
			treeTable = new JTreeTable(treeModel);
		//	treeTable.setBackground(Diagram.DARK_BLUE);

			//	treeTable.setDefaultRenderer( TreeTableModel.class,  new RdfTreeCellRenderer());

			//treeTable.setShowGrid(true);
		//	treeTable.setGridColor(Color.green);
		//	treeTable.setForeground(Color.yellow);

		//	treeTable.setSelectionBackground(Diagram.DARK_BLUE);
		//	treeTable.setSelectionForeground(Color.cyan);

			treeTable.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
			//??
			tree = treeTable.getTree();
			//tree.setForeground(Color.cyan);

		} else {
			tree = new JTree(treeModel);
		}
		initMenu();
		addMouseListeners();
		initTreeComponent();

	}

	public void reload() {
		//init();

		//initTreeModel();
		createTreeModel(getTopModel(), getTopModelFilename());

		getTreeModel().setModel(getTopModel());
		treeModel.reload();

		tree.setModel(treeModel);
		tree.repaint();

	}

	protected void addMouseListeners() {
		TreeMouseListener mouseListener = new TreeMouseListener(this);
		tree.addMouseListener(mouseListener);
	}



	public void initMenu() {
		editMenu = new EditMenu(this);
		popupMenu = editMenu.getPopupMenu();
		// new JPopupMenu();

		tree.add(popupMenu);
		//	tree.addMouseListener(new PopupTrigger(this));
	}

	protected void clear() {
		/*
		try {
			remove(treeScroll);
		} catch (Exception exception) {
			// not there, ignore
		}
		*/
	}

	public void createTreeModel(Model model, String filename) {
		RdfNodeMap nodeMap = new RdfNodeMap(model);
		nodeMap.setTopModelFilename(filename);
		RootNode root = new RootNode(nodeMap);
		setTreeModel(new RdfTreeModel(root, model, nodeMap));
	}

	public Model getTopModel() {
		if (topModel == null) {
			topModel = new ModelMem();
		}
		return topModel;
	}

	public String getTopModelFilename() {
		if (filename != null) {
			return filename;
		}
		return "./temp.rdf";
	}

	protected void initTreeComponent() {
		//tree.setPreferredSize(new Dimension(200, 200));
		tree.setVisible(true);

	//	tree.setBackground(Diagram.DARK_BLUE);

		tree.putClientProperty("JTree.lineStyle", "Angled");

	//	tree.putClientProperty("JTree.lineColor", Color.yellow);

		ToolTipManager.sharedInstance().registerComponent(tree);
		tree.setCellRenderer(getCellRenderer());

		tree.getSelectionModel().setSelectionMode(
			TreeSelectionModel.SINGLE_TREE_SELECTION);
		tree.addTreeSelectionListener(this);

		//	treeScroll = new JScrollPane(tree);
		//	add(treeScroll, BorderLayout.CENTER);

		tree.getPreferredScrollableViewportSize();

		tree.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		// ??

		//	JPanel panel = new JPanel(new GridLayout(1, 1));
		JScrollPane scroll;
		if (withTable) {
			//		panel.add(treeTable);
			scroll = new JScrollPane(treeTable);
			add(scroll);
		} else {
			//	panel.add(tree);
			scroll = new JScrollPane(tree);
			add(scroll);
		}
		//	add(panel);

		//add(treeTable.getTree());
	}

	protected TreeCellRenderer getCellRenderer() {
		return new RdfTreeCellRenderer();
	}

	//Listen for when the selection changes.
	public void valueChanged(TreeSelectionEvent e) {
		Object object = tree.getLastSelectedPathComponent();

		if ((object == null) || !(object instanceof ResourceNode)) {
			return;
		}

		ResourceNode node = (ResourceNode) object;
		selectedUrl = node.getResource().getURI();
		//tree.scrollPathToVisible(path); 
		tree.revalidate();
	}

	public RdfTreeModel getTreeModel() {
		return treeModel;
	}

	public void setTreeModel(RdfTreeModel treeModel) {
		this.treeModel = treeModel;
	}

	public JTree getTree() {
		return tree;
	}

	public void setTree(JTree tree) {
		this.tree = tree;
	}

	public Action getAction() {
		return getEditMenu().getTreeAction();
	}

	public JPopupMenu getPopupMenu() {
		return popupMenu;
	}

	public void setClickedPath(TreePath path) {
		clickedPath = path;
	}

	public void setTopModel(Model topModel) {
		this.topModel = topModel;
	}

	public void setTopModelFilename(String filename) {
		this.filename = filename;
	}

	public void addTreeSelectionListener(TreeSelectionListener listener) {
		tree.addTreeSelectionListener(listener);
	}

	public TreePath getClickedPath() {
		return clickedPath;
	}
	public EditMenu getEditMenu() {
		return editMenu;
	}


}
